<template>
  <aside class="sidebar">
    <div class="author-card">
      <div class="floating-cards">
        <span v-for="n in 12" :key="n" class="card-particle"></span>
      </div>
      <div class="card-header">
        <div class="author-avatar">
          <img :src="$store.state.webSiteInfo.authorAvatar" alt="作者头像">
        </div>
        <div class="status-badge">
          <span>在线</span>
        </div>
      </div>
      <div class="author-info">
        <h3>{{ $store.state.webSiteInfo.author }}</h3>
        <p class="bio">{{ $store.state.webSiteInfo.authorInfo }}</p>
      </div>
      <div class="social-links">
        <mj-tooltip :content="item.content" v-for="item in socialLinks" :key="item.type">
          <a v-if="$store.state.webSiteInfo.showList.indexOf(item.type) !== -1" href="javascript:void(0)" title="GitHub"
            :class="`social-btn ${item.type}`" @click="copyToClipboard(item)">
            <i :class="item.icon"></i>
          </a>
        </mj-tooltip>
      </div>
    </div>

    <div class="section announcement" v-if="visibleCategories">
      <h3>
        <svg t="1735873918498" class="icon" style="transform: translateY(5px);" viewBox="0 0 1024 1024" version="1.1"
          xmlns="http://www.w3.org/2000/svg" p-id="3310" width="22" height="22">
          <path
            d="M585.1648 905.1648c-19.8144 0-35.84-16.0256-35.84-35.84v-283.392c0-19.8144 16.0256-35.84 35.84-35.84s35.84 16.0256 35.84 35.84v283.392c0 19.7632-16.0768 35.84-35.84 35.84zM728.9344 905.1648c-19.8144 0-35.84-16.0256-35.84-35.84v-283.392c0-19.8144 16.0256-35.84 35.84-35.84s35.84 16.0256 35.84 35.84v283.392c0 19.7632-16.0768 35.84-35.84 35.84zM875.7248 905.1648c-19.8144 0-35.84-16.0256-35.84-35.84v-283.392c0-19.8144 16.0256-35.84 35.84-35.84s35.84 16.0256 35.84 35.84v283.392c0 19.7632-16.0768 35.84-35.84 35.84z"
            fill="#FEC963" p-id="3311"></path>
          <path
            d="M234.4448 481.1776h174.7456c35.328 0 64-28.672 64-64v-175.104c0-35.328-28.672-64-64-64H221.5424c-28.2112 0-51.0976 22.8864-51.0976 51.0976v188.0576c0.0512 35.328 28.672 63.9488 64 63.9488zM860.16 178.0736h-187.648c-28.2112 0-51.0976 22.8864-51.0976 51.0976v188.0576c0 35.328 28.672 64 64 64H860.16c35.328 0 64-28.672 64-64v-175.104c0-35.3792-28.672-64.0512-64-64.0512zM410.9824 619.1104H223.3344c-28.2112 0-51.0976 22.8864-51.0976 51.0976v188.0576c0 35.328 28.672 64 64 64h174.7456c35.328 0 64-28.672 64-64v-175.104c0-35.4304-28.672-64.0512-64-64.0512z"
            fill="#9bc6fc" p-id="3312"></path>
          <path
            d="M390.3488 105.0624h-224.768c-38.8096 0-70.4 31.5904-70.4 70.4v224.768c0 38.8096 31.5904 70.4 70.4 70.4h224.768c38.8096 0 70.4-31.5904 70.4-70.4v-224.768c0.0512-38.8608-31.5392-70.4-70.4-70.4zM414.72 400.2304c0 13.4144-10.9056 24.32-24.32 24.32h-224.768c-13.4144 0-24.32-10.9056-24.32-24.32v-224.768c0-13.4144 10.9056-24.32 24.32-24.32h224.768c13.4144 0 24.32 10.9056 24.32 24.32v224.768zM390.3488 546.6112h-224.768c-38.8096 0-70.4 31.5904-70.4 70.4v224.768c0 38.8096 31.5904 70.4 70.4 70.4h224.768c38.8096 0 70.4-31.5904 70.4-70.4v-224.768c0.0512-38.8096-31.5392-70.4-70.4-70.4zM414.72 841.7792c0 13.4144-10.9056 24.32-24.32 24.32h-224.768c-13.4144 0-24.32-10.9056-24.32-24.32v-224.768c0-13.4144 10.9056-24.32 24.32-24.32h224.768c13.4144 0 24.32 10.9056 24.32 24.32v224.768zM841.6256 105.0624h-224.768c-38.8096 0-70.4 31.5904-70.4 70.4v224.768c0 38.8096 31.5904 70.4 70.4 70.4h224.768c38.8096 0 70.4-31.5904 70.4-70.4v-224.768c0-38.8608-31.5904-70.4-70.4-70.4z m24.32 295.168c0 13.4144-10.9056 24.32-24.32 24.32h-224.768c-13.4144 0-24.32-10.9056-24.32-24.32v-224.768c0-13.4144 10.9056-24.32 24.32-24.32h224.768c13.4144 0 24.32 10.9056 24.32 24.32v224.768zM571.4944 545.5872a23.04 23.04 0 0 0-23.04 23.04v317.3888c0 12.7488 10.2912 23.04 23.04 23.04s23.04-10.2912 23.04-23.04v-317.3888c0-12.7488-10.2912-23.04-23.04-23.04zM719.0528 545.5872a23.04 23.04 0 0 0-23.04 23.04v317.3888c0 12.7488 10.2912 23.04 23.04 23.04s23.04-10.2912 23.04-23.04v-317.3888a23.04 23.04 0 0 0-23.04-23.04zM866.56 545.5872a23.04 23.04 0 0 0-23.04 23.04v317.3888c0 12.7488 10.2912 23.04 23.04 23.04s23.04-10.2912 23.04-23.04v-317.3888c0-12.7488-10.2912-23.04-23.04-23.04z"
            fill="#474747" p-id="3313"></path>
        </svg>
        分类
      </h3>
      <div class="announcement-content" style="max-height: 360px;overflow-y: auto;">
        <div class="announcement-item" :class="activeIndex == index ? 'activeIndexItem' : ''"
          v-for="(item, index) in categoriesList" :key="index"
          @click="$emit('getCategoriesList', item.id); activeIndex = index; getTagList(item.id)">
          <i class="fas fa-folder-open" v-if="activeIndex == index"></i>
          <i class="fas fa-folder-open" style="color: rgba(255, 0, 0, 0.5);" v-else></i>
          <span v-html="item.name"></span>
        </div>
      </div>
    </div>

    <!-- 标签墙 -->
    <div class="section announcement tag_container">
      <h3 style="display: flex;align-items: center;">
        <svg t="1735886994589" class="icon" viewBox="0 0 1025 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2782" width="22" height="22"><path d="M0.257086 118.094877C-3.815151 57.01132 40.979458 5.42965 102.063015 0h16.288948l393.649591 9.501887c25.790835 1.357412 50.224258 12.216711 67.870619 31.220484l414.010775 414.010776c42.079784 42.079784 42.079784 111.307815-1.357412 153.387599L608.377833 992.26845c-42.079784 42.079784-109.950403 42.079784-153.387599 1.357412L40.979458 578.257674c-19.003773-17.646361-29.863072-42.079784-31.220485-67.870619L0.257086 118.094877z" fill="#2F77F1" p-id="2783"></path><path d="M233.732016 225.330455m-108.592991 0a108.59299 108.59299 0 1 0 217.185981 0 108.59299 108.59299 0 1 0-217.185981 0Z" fill="#AFFCFE" p-id="2784"></path></svg>
        <div class="clearfix" style="width: 100%;margin-left: 5px;">
          <span class="card-title"> 标签墙</span>
          <router-link :to="'/tags'">
            <a class="more hand-style no-select">更多</a>
          </router-link>
        </div>
      </h3>
      <div class="announcement-content">
        <div style="display: flex;flex-wrap: wrap;">
          <span @click="$emit('getTagsList', item.id);" :style="{
          backgroundColor: `${randomColor()}`,
          fontSize: item.font
        }" class="tag-item hand-style" v-for="(item, index) in tags" :key="index">
            {{ item.name }}
          </span>
        </div>
      </div>
    </div>

    <div class="section announcement">
      <h3>
        <i class="fas fa-bullhorn"></i>
        公告
      </h3>
      <div class="announcement-content">
        <div class="announcement-item" v-for="(item, index) in announcements" :key="index">
          <i :class="item.icon"></i>
          <span v-html="item.content"></span>
        </div>
      </div>
    </div>

    <div class="section" v-if="hot.length > 0">
      <h3>推荐文章</h3>
      <div class="post-list">
        <router-link v-for="post in hot" :key="post.id" :to="`/post/${post.id}`" class="post-item">
          <img :src="post.cover" :alt="post.title" @error="handleImageError">
          <div class="post-meta">
            <h4>{{ post.title }}</h4>
            <time>{{ post.createTime }}</time>
          </div>
        </router-link>
      </div>
    </div>

    <!-- <div class="section">
      <h3>标签</h3>
      <div class="tags">
        <a 
          v-for="tag in tags" 
          :key="tag.name"
          :href="`#${tag.name}`"
          class="tag"
        >
          {{ tag.name }}
        </a>
      </div>
    </div> -->
  </aside>
</template>

<script>
import { getRecommendArticlesApi, getCategoriesApi } from '@/api/article'
import { getTagsApi } from '@/api/tags'

export default {
  name: 'Sidebar',
  data() {
    return {
      hot: [],
      announcements: [
        {
          icon: 'fas fa-star',
          content: this.$store.state.webSiteInfo.bulletin
        },
        // {
        //   icon: 'fas fa-code',
        //   content: '本站基于 Vue2 + Spring Boot 开发，没有使用任何UI框架，源码已开源。'
        // },
        // {
        //   icon: 'fas fa-envelope',
        //   content: '有问题欢迎通过邮箱或其他社交方式联系我。'
        // }
      ],
      categoriesList: [],
      activeIndex: 0,
      visibleCategories: false,
      socialLinks: [
        {
          icon: 'fab fa-github',
          type: 'github',
          link: this.$store.state.webSiteInfo.github,
          content: '点击跳转GitHub主页',
          icCopy: false
        },
        {
          icon: 'fab fa-git-alt',
          type: 'gitee',
          link: this.$store.state.webSiteInfo.gitee,
          content: '点击跳转GitEE主页',
          icCopy: false
        },

        {
          icon: 'fab fa-qq',
          title: 'QQ',
          type: 'qq',
          link: this.$store.state.webSiteInfo.qqNumber,
          content: '点击复制QQ号',
          icCopy: true
        },
        {
          icon: 'fas fa-users',
          title: 'QQ群',
          type: 'qqGroup',
          link: this.$store.state.webSiteInfo.qqGroup,
          content: '点击复制QQ群号',
          icCopy: true
        },
        {
          icon: 'fas fa-at',
          title: '邮箱',
          type: 'email',
          link: this.$store.state.webSiteInfo.email,
          content: '点击复制邮箱',
          icCopy: true
        },
        {
          icon: 'fab fa-weixin',
          title: '微信',
          type: 'wechat',
          link: this.$store.state.webSiteInfo.wechat,
          content: '点击复制微信号',
          icCopy: true
        }
      ],
      tags: [],
    }
  },
  mounted() {
    this.visibleCategories = this.$route.path == '/'
    getCategoriesApi().then(res => {
      this.categoriesList = res.data
      this.categoriesList.unshift({
        name: '全部',
        id: null
      })
    })
    this.getTagList(null)
    getRecommendArticlesApi().then(res => {
      this.hot = res.data
    })
    setTimeout(() => {
      this.announcements[0].content = this.$store.state.webSiteInfo.bulletin
    }, 200)
  },
  methods: {
    /**
     * 处理图片加载失败
     */
    handleImageError(e) {
      e.target.src = this.$store.state.defaultImage
      e.target.classList.add('fallback')
    },
    /**
     * 复制到剪贴板
     */
    copyToClipboard(item) {
      if (item.icCopy) {
        navigator.clipboard.writeText(item.title).then(() => {
          this.$message.success(`${item.title.toUpperCase()}账号已复制到剪贴板`);
        }).catch(() => {
          this.$message.error('复制失败，请手动复制');
        });
      } else {
        window.open(item.link, '_blank')
      }
    },
    async getTagList(categoryId) {
      const res = await getTagsApi({ categoryId })
      this.tags = res.data
      console.log(this.tags);
      for (var i = 0; i < this.tags.length; i++) {
        this.tags[i].font = Math.floor(Math.random() * 10) + 10 + "px";
      }
    },
    //随机颜色
    randomColor() {
      var letters = "0123456789ABCDEF";
      var color = "#";
      do {
        for (var i = 0; i < 6; i++) {
          color += letters[Math.floor(Math.random() * 16)];
        }
      } while (color === "#FFFFFF" || color === "#000000");
      return color;
    }
  },
  computed: {
    currentRoute() {
      return this.$route.path;
    }
  },
}
</script>

<style lang="scss" scoped>
.sidebar {
  position: sticky;
  top: 80px;
  width: 100%;
  max-width: 320px;

  .author-card {
    background: var(--card-bg);
    border-radius: $border-radius-md;
    padding: $spacing-lg;
    margin-bottom: $spacing-lg;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
    position: relative;

    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      height: 120px;
      background-image: url('https://img.shiyit.com/beijing.jpg');
      background-size: cover;
      background-position: center;
      transition: opacity 0.3s ease;
      border-top-left-radius: $border-radius-md;
      border-top-right-radius: $border-radius-md;
    }

    &:hover::before {
      opacity: 0.2;
    }

    .card-header {
      position: relative;
      margin-bottom: 20px;
      display: flex;
      justify-content: center;

      .author-avatar {
        width: 88px;
        height: 88px;
        position: relative;

        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          object-fit: cover;
          border: 3px solid #fff;
          box-shadow: 0 4px 12px rgba(99, 102, 241, 0.15);
          transition: transform 0.3s ease;

          &:hover {
            transform: scale(1.05);
          }
        }
      }

      .status-badge {
        position: absolute;
        bottom: 0;
        right: 50%;
        transform: translateX(32px);
        background: linear-gradient(135deg, #10b981, #059669);
        border-radius: 12px;
        padding: 4px 12px;
        font-size: 0.75rem;
        color: white;
        box-shadow: 0 4px 12px rgba(16, 185, 129, 0.2);

        span {
          display: flex;
          align-items: center;
          gap: 4px;

          &::before {
            content: '';
            display: inline-block;
            width: 6px;
            height: 6px;
            border-radius: 50%;
            background: #fff;
          }
        }
      }
    }

    .author-info {
      text-align: center;
      margin-bottom: 20px;

      h3 {
        font-size: 1.25rem;
        font-weight: 700;
        color: $primary;
        -webkit-background-clip: text;
        margin-bottom: 8px;
      }

      .bio {
        font-size: 0.9rem;
        color: var(--text-secondary);
        line-height: 1.6;
        margin-bottom: 16px;
      }
    }

    .social-links {
      display: flex;
      justify-content: center;
      gap: 10px;
      margin-top: 20px;

      .social-btn {
        width: 36px;
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 12px;
        background: rgba(99, 102, 241, 0.05);
        font-size: 1.1rem;
        transition: all 0.3s ease;
        position: relative;
        text-decoration: none;
      }

      .qq {
        color: #60a5fa;

        &:hover {
          background: #60a5fa;
          color: white;
        }
      }

      .qqGroup {
        color: #e1c235;

        &:hover {
          background: #e1c235;
          color: white;
        }
      }

      .github {
        color: #000;

        &:hover {
          background: #000;
          color: white;
        }
      }

      .gitee {
        color: #ee3434;

        &:hover {
          background: #da3535;
          color: white;
        }
      }

      .email {
        color: #d872a7;

        &:hover {
          background: #d872a7;
          color: white;
        }
      }

      .wechat {
        color: #10b981;

        &:hover {
          background: #10b981;
          color: white;
        }
      }
    }

    .floating-cards {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      pointer-events: none;
      overflow: hidden;
      opacity: 0;
      transition: opacity 0.3s ease;
    }

    &:hover .floating-cards {
      opacity: 1;
    }

    .card-particle {
      position: absolute;
      width: 8px;
      height: 8px;
      background: white;
      border-radius: 2px;
      opacity: 0;
      animation: fall 3s ease-in-out infinite;

      @for $i from 1 through 12 {
        &:nth-child(#{$i}) {
          left: #{random(100)}#{"%"};
          animation-delay: #{random(3000)}ms;
          background: #{nth(
 (#6366f1,
            #8b5cf6,
            #ec4899,
            #10b981,
            #f59e0b,
            #ef4444),
          random(6))
        }

        ;
        transform: rotate(#{$i * 30}deg);
      }
    }
  }
}

.section {
  background: var(--card-bg);
  border-radius: $border-radius-md;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
  padding: $spacing-lg;
  margin-bottom: $spacing-lg;

  h3 {
    font-size: 1.1rem;
    font-weight: 600;
    color: #6366f1;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 2px solid rgba(99, 102, 241, 0.1);

    &::before {
      content: '';
      display: inline-block;
      width: 4px;
      height: 16px;
      background: linear-gradient(to bottom, #6366f1, #8b5cf6);
      margin-right: 8px;
      border-radius: 2px;
      vertical-align: middle;
      transform: translateY(-1px);
    }
  }

  .post-list {
    display: flex;
    flex-direction: column;
    gap: 20px;
    counter-reset: post-counter;

    .post-item {
      display: flex;
      gap: 16px;
      text-decoration: none;
      transition: all 0.3s ease;
      position: relative;
      padding-left: 32px;

      &::before {
        content: counter(post-counter);
        counter-increment: post-counter;
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 22px;
        height: 22px;
        background: var(--number-bg, #f87171);
        color: white;
        border-radius: 6px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 13px;
        font-weight: 600;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      }

      &:nth-child(2)::before {
        --number-bg: #fbbf24;
      }

      &:nth-child(3)::before {
        --number-bg: #60a5fa;
      }

      &:nth-child(n+4)::before {
        --number-bg: #9ca3af;
      }

      &:hover {
        transform: translateX(4px);

        h4 {
          color: #6366f1;
        }

        img {
          transform: scale(1.03);
        }
      }

      img {
        width: 100px;
        height: 70px;
        border-radius: 6px;
        object-fit: cover;
        transition: transform 0.3s ease;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);

        &.fallback {
          opacity: 0.7;
        }
      }

      .post-meta {
        flex: 1;
        min-width: 0;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        h4 {
          font-size: 0.95rem;
          font-weight: 500;
          color: var(--text-primary);
          margin-bottom: 6px;
          overflow: hidden;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          line-height: 1.4;
          transition: color 0.3s ease;
        }

        time {
          font-size: 0.8rem;
          color: #8b8b8b;
          display: flex;
          align-items: center;
          gap: 4px;

          &::before {
            content: '\f017';
            font-family: 'Font Awesome 5 Free';
            font-size: 0.75rem;
            opacity: 0.8;
          }
        }
      }
    }
  }

  .tags {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;

    .tag {
      font-size: 0.9rem;
      color: #6366f1;
      text-decoration: none;
      padding: 4px 12px;
      border-radius: 16px;
      background: rgba(99, 102, 241, 0.05);
      border: 1px solid rgba(99, 102, 241, 0.1);
      font-weight: 500;
      transition: all 0.2s;

      &:hover {
        background: linear-gradient(135deg, #6366f1, #8b5cf6);
        color: white;
        border-color: transparent;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(99, 102, 241, 0.2);
      }
    }
  }
}

.announcement {
  h3 {
    i {
      margin-right: 8px;
      color: #f59e0b;
      animation: shake 1.5s ease-in-out infinite;
    }
  }

  .announcement-content {
    .announcement-item {
      display: flex;
      align-items: flex-start;
      gap: 12px;
      padding: 12px 0;
      border-bottom: 1px dashed rgba(99, 102, 241, 0.1);
      cursor: pointer;

      &:last-child {
        border-bottom: none;
        padding-bottom: 0;
      }

      &:first-child {
        padding-top: 0;
      }

      i {
        font-size: 1rem;
        color: #6366f1;
        flex-shrink: 0;
        margin-top: 3px;
      }

      span {
        font-size: 0.9rem;
        color: var(--text-secondary);
        line-height: 1.6;
      }

      &:hover {
        i {
          transform: scale(1.1);
        }

        span {
          color: var(--text-primary);
        }
      }

      i,
      span {
        transition: all 0.3s ease;
      }
    }

    .activeIndexItem {
      span {
        color: #6366f1 !important;
      }
    }
  }
}

.tag_container {
  font-size: 0.9rem;

  &:hover {
    transition: all 0.3s;
  }

  .clearfix {
    font-size: 16px;

    .more {
      float: right;
      margin-right: 10px;
      font-size: 13px;
      color: var(--text-secondary);
      text-decoration: none;

      &:hover {
        color: var(--text-primary);
      }
    }
  }

  .tagBox {
    padding-bottom: 10px;
    min-height: 120px;
    margin-top: 10px;
  }

  >div span {
    margin: 2px;
    padding: 3px;
    color: #fff;
    cursor: pointer;
    text-align: center;
    display: flex;
    align-items: center;

    &:hover{
      color: var(--text-primary);
    }
  }
}

@media (prefers-color-scheme: dark) {
  .sidebar {
    .section {
      .post-item {
        &::before {
          box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        }

        img {
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
        }

        .post-meta {
          time {
            color: #777;
          }
        }

        &:hover h4 {
          color: #818cf8;
        }
      }
    }
  }
}
}

@include responsive(lg) {
  .sidebar {
    display: none;
  }
}

@keyframes float {
  0% {
    transform: translateY(0px);
  }

  50% {
    transform: translateY(-5px);
  }

  100% {
    transform: translateY(0px);
  }
}

.author-avatar img {
  animation: float 3s ease-in-out infinite;
}

@keyframes fall {
  0% {
    transform: translateY(-20px) rotate(0deg);
    opacity: 0;
  }

  20% {
    opacity: 0.8;
  }

  80% {
    opacity: 0.8;
  }

  100% {
    transform: translateY(300px) rotate(360deg);
    opacity: 0;
  }
}

@keyframes shake {
  0% {
    transform: rotate(0deg);
  }

  25% {
    transform: rotate(-10deg);
  }

  75% {
    transform: rotate(10deg);
  }

  100% {
    transform: rotate(0deg);
  }
}
</style>